package seismosurfer.servlets.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.text.DecimalFormat;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import seismosurfer.data.constants.QueryNames;
import seismosurfer.util.SeismoException;

/**
 * A Utility that provides various operations
 * needed by the servlets.
 *
 */
public class ServletUtil implements QueryNames {

    public static final String BINARY_CONTENT = "application/x-java-serialized-object";

    public static final String APPLET_FILE_PATH = "/sSeismoSurfer.jar";

    public ServletUtil() {

    }
    
    /**
     * Gets a cookie given its name and an array of cookies.
     * 
     * @param cookies the array of Cookie objects
     * @param cookieName the name of the cookie
     * @return a Cookie object or null
     */
    public static Cookie getCookie(Cookie[] cookies, String cookieName) {
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                Cookie cookie = cookies[i];
                if (cookieName.equals(cookie.getName())) {
                    return (cookie);
                }
            }
        }
        return null;

    }
    
    /**
     * Sends an error message to the web client.
     * 
     * @param response the HttpServletResponse object that
     *        will be used for the response
     */
    public static void displayUserError(HttpServletResponse response) {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    }
    
    /**
     * Sends an object to the applet using object serialization
     * through HTTP.
     * 
     * @param response the HttpServletResponse object that
     *        will be used for the response
     * @param value the Object that will be send to the applet
     */
    public static void sendBinaryResponse(HttpServletResponse response,
            Object value) {

        try {

            // Set the Content-Type header
            response.setContentType(BINARY_CONTENT);

            // Buffer the data
            ByteArrayOutputStream byteStream = new ByteArrayOutputStream(512);

            // Attach an Object Output Stream to the ByteArrayOutputStream
            ObjectOutputStream out = new ObjectOutputStream(byteStream);

            // Put the data into the buffer
            out.writeObject(value);
            out.flush();

            // Set the Content-Length header
            response.setContentLength(byteStream.size());

            // Send the real data
            byteStream.writeTo(response.getOutputStream());
        } catch (IOException e) {
            System.out.println("IOException in ServletsUtil.sendObject :"
                    + e.getMessage());
            e.printStackTrace();
        }

    }
    
    /**
     * Gets the size of the applet jar file.
     * 
     * @return the String that contains the size of the applet
     */
    public static String getAppletSize() {
        double appletSize = getResourceSize(APPLET_FILE_PATH) / Math.pow(2, 20);
        DecimalFormat dec = new DecimalFormat("#.#");
        String appSize = dec.format(appletSize);

        return appSize;
    }
    
    /**
     * Gets the size of a given resource (file).
     * 
     * @param path the path to the resource
     * @return the size of the resource 
     */
    public static int getResourceSize(String path) {
        int size = 0;

        try {
            InputStream in = SessionHandler.getServletContext()
                    .getResourceAsStream(path);
            size = in.available();

            return size;
        } catch (IOException e) {
            throw new SeismoException(e);
        }
    }

}
